import {
    UIGSMgr
} from "../core_for_guosheng/UIGSMgr";
import {
    Utils
} from "../core_for_guosheng/Utils";
import {
    DataGS
} from "../core_for_guosheng/DataGS";
import {
    Config
} from "../core_for_guosheng/Config";
import OnGSEvent from "../core_for_guosheng/OnGSEvent";
import {
    Common
} from "./Common";
import {
    GSJsonMgr
} from "../core_for_guosheng/GSJsonMgr";
// import { ADMgr } from "../core/AdMgr";
import {
    EventKey,
} from "./Key";
import {
    GSPartner
} from "../core_for_guosheng/GSPartner";
import {
    AudioGS
} from "../core_for_guosheng/AudioGS";



cc.Class({
    extends: cc.Component,

    properties: {
        layerScene: cc.Node,
        layerPanel: cc.Node,
        layerToast: cc.Node,
        layerBlockInput: cc.Node,
        pfbToast: cc.Prefab,
    },

    onLoad() {
        window.F = window.F || {};
        F.Game = this;

        this._requireFuckCores();

        this._initFuckToastPool();

        this._initFuckCanvasFit();

        this._registFuckEvents();

        this._loadFuckSceneBoot();
    },

    // 屏幕适配
    _initFuckCanvasFit() {
        const canvas = this.node.getComponent(cc.Canvas);
        let designScale = canvas.designResolution.width / canvas.designResolution.height;
        if (designScale <= cc.view.getCanvasSize().width / cc.view.getCanvasSize().height) {
            canvas.fitHeight = true;
            canvas.fitWidth = false;
        } else {
            canvas.fitHeight = false;
            canvas.fitWidth = true;
        }
    },

    _requireFuckCores() {
        F.Ui = UIGSMgr;
        F.Ui.setRoot(this.layerPanel);

        F.Utils = Utils;
        F.Data = DataGS;
        F.Config = Config;
        F.OnEvent = new OnGSEvent();
        F.Common = Common;
        F.json = GSJsonMgr;
        // F.FuckAd = ADMgr;
        F.GSPartner = GSPartner;
        F.GSPartner.init(null, null);
        F.Audio = AudioGS;
        F.Audio.init();
    },

    _registFuckEvents() {
        F.OnEvent.on(EventKey.EVENT_BLOCK_INPUT, this._blockInput, this);
        F.OnEvent.on(EventKey.EVENT_CANCEL_BLOCK_INPUT, this._cancelBlockInput, this);
    },

    _loadFuckSceneBoot() {
        this.loadScene('SceneBoot');
    },

    ////////////////////////////////toast//////////////////////////
    _initFuckToastPool() {
        F.Utils.createPool('nodePoolToast', this.pfbToast, 1);
    },

    toast(content, delay) {
        let toast = F.Utils.getPoolFuckNode('nodePoolToast');
        if (toast) {
            toast.parent = this.layerToast;
            toast.getComponent('ItemToast').init(content, delay);
        }
    },
    ////////////////////////////////toast//////////////////////////

    _blockInput() {
        this.layerBlockInput.active = true;
    },
    _cancelBlockInput() {
        this.layerBlockInput.active = false;
    },

    ////////////////////////////////场景////////////////////////////
    getCurrScene() {
        return this.mCurrScene;
    },

    loadScene(targetScene, cb) {
        // 传入空的名字，不做任何操作
        if (!targetScene) {
            return;
        }
        if (this.isSceneLoading) {
            return;
        }
        // 已经在当前场景，不做任何操作（）
        if (targetScene === this.mCurrScene) {
            return;
        }

        this.isSceneLoading = true;
        this._blockInput();
        let url = 'scenes_for_guosheng/' + targetScene;
        cc.loader.loadRes(url, cc.Prefab, (err, prefab) => {
            if (err) {
                console.log('加载场景:失败' + err);
                this.cancelBlockInput();
                return;
            }

            if (this.mCurrScene) {
                let oldScene = this.layerScene.getChildByName(this.mCurrScene);
                if (oldScene) {
                    oldScene.destroy();
                }
            }

            this.mCurrScene = targetScene;

            let node = cc.instantiate(prefab);
            node.parent = this.layerScene;

            this.isSceneLoading = false;
            cb && cb();
            F.Ui.closeAllUI();
            this._cancelBlockInput();
        })
    },

    preloadScene(sceneName, progressCallback, cb) {
        let url = 'scenes_for_guosheng/' + sceneName;
        cc.loader.loadRes(url, cc.Prefab, progressCallback, (err, prefab) => {
            if (err) {
                console.log(err)
                this.toast('预加载场景' + sceneName + '失败');
                return;
            }
            cb && cb();
        });
    },

    // update (dt) {},
});